//
//  ZXView.m
//  BezierDemo
//
//  Created by yuanjiang on 2017/12/15.
//  Copyright © 2017年 yuanjiang. All rights reserved.
//

#import "ZXView.h"

@interface ZXView ()

/** */
@property(nonatomic,strong) CAShapeLayer *lineChartLayer;
/** */
@property(nonatomic,strong) UIBezierPath *path1;
/** 渐变背景视图*/
@property(nonatomic,strong) UIView *gradientBackgroundView;

/** 渐变图层*/
@property(nonatomic,strong) CAGradientLayer *gradientLayer;

/** 颜色数组*/
@property(nonatomic,strong) NSMutableArray *gradientLayerColors;

@end

static CGFloat bounceX = 20;
static CGFloat bounceY = 20;
static NSInteger countq = 0;

@implementation ZXView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self creatLabelX];
        [self creatlabelY];
        [self drawGradientBackgrogundView];
        [self setLineDash];
        
    }
    return self;
}
#pragma mark - 创建x轴数据
-(void)creatLabelX
{
    CGFloat month = 12;
    for (NSInteger i = 0; i< month; i++) {
        UILabel *labelMonth = [[UILabel alloc] initWithFrame:CGRectMake((self.frame.size.width - 2 * bounceX) / month * i + bounceX, self.frame.size.height - bounceY + bounceY * 0.3, (self.frame.size.width - 2 * bounceY)/month - 5, bounceY /2)];
        labelMonth.tag = 1000 + i;
        labelMonth.text = [NSString stringWithFormat:@"%ld月", i + 1];
        labelMonth.transform = CGAffineTransformMakeRotation(M_PI * 0.3);
        labelMonth.font = [UIFont systemFontOfSize:10];
        [self addSubview:labelMonth];
    }
}

#pragma mark - 创建y轴数据
-(void)creatlabelY
{
    CGFloat ydivision = 6;
    for (NSInteger i = 0 ; i < ydivision; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, (self.frame.size.height - 2 * bounceY)/ydivision * i+ bounceY , (self.frame.size.height - 2 * bounceY)/ydivision, bounceY / 2.0)];
        label.font = [UIFont systemFontOfSize:10.0f];
        label.tag = 2000 + i;
        label.text = [NSString stringWithFormat:@"%.0f",(ydivision - i) * 100];
        [self addSubview:label];
    }
}
#pragma mark - 渐变的颜色
-(void)drawGradientBackgrogundView
{
  //渐变背景视图（不包含坐标轴）
    self.gradientBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(bounceX, bounceY, self.frame.size.width - bounceY * 2, self.frame.size.height - bounceX *2)];
    [self addSubview:self.gradientBackgroundView];
    //创建并设置渐变背景图层
    //初始化CAGradientlayer对象， 使它的大小为渐变背景视图的代大小
    self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = self.gradientBackgroundView.bounds;
    //渐变区域的起始和终止位置（范围为0-1）,即渐变路径
    self.gradientLayer.startPoint = CGPointMake(0, 0);
    self.gradientLayer.endPoint = CGPointMake(1, 0);
    //设置颜色渐变过程
    self.gradientLayerColors = [NSMutableArray arrayWithArray:@[(__bridge id)[UIColor colorWithRed:253 / 255.0 green:164 / 255.0 blue:8 / 255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:251 / 255.0 green:37 / 255.0 blue:45 / 255.0 alpha:1.0].CGColor]];
    self.gradientLayer.colors = self.gradientLayerColors;
    //将CAGradientLayer对象添加在我们要设置背景色的视图的layer层
    [self.gradientBackgroundView.layer addSublayer:self.gradientLayer];
}

#pragma mark - 添加虚线
-(void)setLineDash
{
    for (NSInteger i = 0; i< 6; i++) {
        CAShapeLayer *dashLayer = [CAShapeLayer layer];
        dashLayer.strokeColor = [UIColor whiteColor].CGColor;
        dashLayer.fillColor = [UIColor clearColor].CGColor;
        //默认设置路径宽度为0，使其在起始状态下不显示
        dashLayer.lineWidth = 1.0;
        
        UILabel *lab = (UILabel *)[self viewWithTag:2000 + i];
        UIBezierPath *path = [[UIBezierPath alloc] init];
        path.lineWidth = 1.0f;
        UIColor *color = [UIColor blueColor];
        [color set];
        [path moveToPoint:CGPointMake(0, lab.frame.origin.y - bounceY)];
        [path addLineToPoint:CGPointMake(self.frame.size.width - bounceX * 2, lab.frame.origin.y - bounceY)];
        CGFloat dash[] = {10,10};
        [path setLineDash:dash count:2 phase:10];
        [path stroke];
        dashLayer.path = path.CGPath;
        [self.gradientBackgroundView.layer addSublayer:dashLayer];
    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    //画坐标轴
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    CGContextMoveToPoint(context, bounceX, bounceY);
    CGContextAddLineToPoint(context, bounceX, rect.size.height - bounceY);
    CGContextAddLineToPoint(context, rect.size.width -  bounceX, rect.size.height - bounceY);
    CGContextStrokePath(context);
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    countq ++;
    if (countq%2 == 0) {
        [self.lineChartLayer removeFromSuperlayer];
        for (NSInteger i = 0; i < 12; i++) {
            UILabel *label = (UILabel *)[self viewWithTag:3000 + i];
            [label removeFromSuperview];
        }
    }else{
        [self deavLine];
        self.lineChartLayer.lineWidth = 2;
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 3;
        pathAnimation.repeatCount = 1;
        pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
        pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
        [self.lineChartLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    }
}

/**
 画折线图
 */
-(void)deavLine
{
    UILabel *lab =  (UILabel *)[self viewWithTag:1000];
    UIBezierPath *path = [[UIBezierPath alloc] init];
    path.lineWidth = 1.0f;
    self.path1 = path;
    UIColor *color = [UIColor greenColor];
    [color set];
    [path moveToPoint:CGPointMake(lab.frame.origin.x - bounceX, (600 - arc4random()%600) / 600*(self.frame.size.height - bounceY * 2))];
    //创建折线点标记
    for (NSInteger i = 0 ; i< 12; i++) {
        UILabel *label1 = (UILabel *)[self viewWithTag:1000 + i];
        CGFloat arc =  arc4random()%600;
        [path addLineToPoint:CGPointMake(label1.frame.origin.x - bounceX, (600 - arc) / 600 * (self.frame.size.height - bounceY*2))];
    }
    self.lineChartLayer = [CAShapeLayer layer];
    self.lineChartLayer.path = path.CGPath;
    self.lineChartLayer.strokeColor = [UIColor whiteColor].CGColor;
    self.lineChartLayer.fillColor = [UIColor clearColor].CGColor;
    self.lineChartLayer.lineWidth = 0;
    self.lineChartLayer.lineCap = kCALineCapRound;
    self.lineChartLayer.lineJoin = kCALineJoinRound;
    [self.gradientBackgroundView.layer addSublayer:self.lineChartLayer];
}



@end
